// pages/communitySystem/list/list.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navType: 2,
        list: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        uploadSelect: false,
        videoPlayIndex: null,
        isShow: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getIsShow()
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            list: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getList()
    },

    myCatchTouch: function() {
        return
    },

    getIsShow: function() {
        const that = this
        common.post('Home/get_index', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                return
            }
            if (res == app.globalData.isShow) {
                that.setData({
                    isShow: true
                })
            } else {
                that.setData({
                    isShow: false
                })
            }
        }).catch(function(e) {})
    },

    getList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let list = this.data.list
        let data = {
            activity: 785,
            type: this.data.navType,
            order_by: 'add_time',
            limit: 10,
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Moments/moment_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    list: list,
                    isAll: true
                })
            } else {
                list = list.concat(res)
                that.setData({
                    list: list,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.navType == e.currentTarget.dataset.type) {
            return
        }
        if (this.data.isLoading) {
            return
        }
        this.setData({
            navType: e.currentTarget.dataset.type,
            list: [],
            nowPage: 0,
            isAll: false,
            isLoading: false
        })
        this.getList()
    },

    goDetail: function(e) {
        wx.navigateTo({
            url: '/pages/communitySystem/detail/detail?id=' + e.currentTarget.dataset.id
        })
    },

    showUploadSelect: function() {
        this.setData({
            uploadSelect: true
        })
    },

    closeUploadSelect: function() {
        this.setData({
            uploadSelect: false
        })
    },

    uploadVideo: function() {
        this.setData({
            uploadSelect: false
        })
        wx.navigateTo({
            url: '/pages/communitySystem/write/write?type=video'
        })
    },

    uploadImage: function() {
        this.setData({
            uploadSelect: false
        })
        wx.navigateTo({
            url: '/pages/communitySystem/write/write?type=image'
        })
    },

    donothing: function () {
        return
    },

    videoPlay: function(e) {
        if (this.data.videoPlayIndex == null || this.data.videoPlayIndex == e.currentTarget.dataset.index) {
            this.setData({
                videoPlayIndex: e.currentTarget.dataset.index
            })
        } else {
            let videoContextPrev = wx.createVideoContext('video' + this.data.videoPlayIndex)
            videoContextPrev.pause()
            this.setData({
                videoPlayIndex: e.currentTarget.dataset.index
            })
        }
    }
})